package com.xoa.controller.cms;

import com.xoa.model.cms.CmsChannelInfo;
import com.xoa.model.enclosure.Attachment;
import com.xoa.service.cms.CmsChannelService;
import com.xoa.util.common.wrapper.BaseWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("cmsChannel")
public class CmsChannelController {
    @Autowired
    CmsChannelService cmsChannelService;

    @ResponseBody
    @RequestMapping("/addChannelInfo")
    public BaseWrapper addChannelInfo(HttpServletRequest request, CmsChannelInfo channelInfo){
        BaseWrapper baseWrapper=new BaseWrapper();
        cmsChannelService.addChannelInfo(request,channelInfo);
        String attachmentSidStr = request.getParameter("channelImg");
        /*List<Attachment> attachments = cmsChannelService.getAttachmentDao().getAttachmentsByIds(attachmentSidStr);
        if(attachments!= null && attachments.size()>0){
            for(TeeAttachment attach:attachments){
                attach.setModelId(String.valueOf(channelInfo.getSid()));
                channelService.getSimpleDaoSupport().update(attach);
            }
        }*/
        baseWrapper.setFlag(true);
        baseWrapper.setMsg("成功");
        baseWrapper.setStatus(true);
        return baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/updateChannelInfo")
    public BaseWrapper updateChannelInfo(HttpServletRequest request , CmsChannelInfo cmsChannelInfo){
        BaseWrapper baseWrapper = new BaseWrapper();
        String attachmentSidStr = request.getParameter("channelImg");

       /* List<TeeAttachment> attachments = channelService.getAttachmentDao().getAttachmentsByIds(attachmentSidStr);
        if(attachments!= null && attachments.size()>0){
            for(TeeAttachment attach:attachments){
                attach.setModelId(String.valueOf(channelInfo.getSid()));
                channelService.getSimpleDaoSupport().update(attach);
            }
        }*/
        cmsChannelService.updateChannelInfo(cmsChannelInfo);
        baseWrapper.setMsg("ok");
        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        return baseWrapper;
    }

    @ResponseBody
    @RequestMapping("/getChannelInfo")
    public BaseWrapper getChannelInfo(HttpServletRequest request, CmsChannelInfo channelInfo){
        BaseWrapper json = new BaseWrapper();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        CmsChannelInfo model = new CmsChannelInfo();
        CmsChannelInfo info = cmsChannelService.getChannelInfo(channelInfo.getSid());
        BeanUtils.copyProperties(info, model);
        if(null!=info.getCrTime()){
            model.setCrTimeStr(sf.format(info.getCrTime().getTime()));
        }
        List<Attachment> attachmodels = new ArrayList<Attachment>();
       /* List<Attachment> attaches =channelService.getAttachmentDao().getAttaches(TeeAttachmentModelKeys.cmsChannel, String.valueOf(channelInfo.getSid()));
        for (Attachment attach : attaches) {
            Attachment attachmentModel = new Attachment();
            BeanUtils.copyProperties(attach, attachmentModel);
            attachmentModel.setuser(attach.getUser().getUuid() + "");
            attachmentModel.setUserName(attach.getUser().getUserName());
            attachmentModel.setPriv(1+2+4+8+16+32);// 一共五个权限好像
            // 1、2、4、8、16、32,具体权限值含义可以参考TeeAttachment
            attachmodels.add(attachmentModel);
        }*/
        model.setAttachmentList(attachmodels);
        json.setData(model);
        json.setStatus(true);
        json.setFlag(true);
        return json;
    }

    @ResponseBody
    @RequestMapping("/delChannelInfo")
    public BaseWrapper delChannelInfo(HttpServletRequest request, @RequestParam(required = false, value = "paths[]") List<String> paths){
        BaseWrapper json = new BaseWrapper();
        cmsChannelService.delChannelInfo(paths);
        json.setFlag(true);
        json.setStatus(true);
        json.setMsg("成功");
        return json;
    }

    @ResponseBody
    @RequestMapping("/moveToTrash")
    public BaseWrapper moveToTrash(HttpServletRequest request, @RequestParam(required = false, value = "sids[]") List<Integer> sids){
        BaseWrapper json = new BaseWrapper();
        cmsChannelService.moveToTrash(sids);
        json.setFlag(true);
        json.setStatus(true);
        json.setMsg("成功");
        return json;
    }

    @ResponseBody
    @RequestMapping("/recycle")
    public BaseWrapper recycle(HttpServletRequest request, CmsChannelInfo channelInfo){
        BaseWrapper json = new BaseWrapper();
        cmsChannelService.recycle(channelInfo);
        json.setFlag(true);
        json.setStatus(true);
        json.setMsg("成功");
        return json;
    }

    @ResponseBody
    @RequestMapping("/clearTrash")
    public BaseWrapper clearTrash(HttpServletRequest request, Integer siteId, Integer channelId){
        BaseWrapper json = new BaseWrapper();
        cmsChannelService.clearTrash(siteId, channelId);
        json.setFlag(true);
        json.setStatus(true);
        json.setMsg("成功");
        return json;
    }

    @ResponseBody
    @RequestMapping("/moveChannel")
    public BaseWrapper moveChannel(HttpServletRequest request, CmsChannelInfo channelInfo){
        BaseWrapper json = new BaseWrapper();
        cmsChannelService.moveChannel(channelInfo);
        json.setFlag(true);
        json.setStatus(true);
        json.setMsg("成功");
        return json;
    }

    @ResponseBody
    @RequestMapping("/copyChannel")
    public BaseWrapper copyChannel(HttpServletRequest request, CmsChannelInfo channelInfo){
        BaseWrapper json = new BaseWrapper();
        cmsChannelService.copyChannel(channelInfo);
        json.setFlag(true);
        json.setStatus(true);
        json.setMsg("成功");
        return json;
    }

    @ResponseBody
    @RequestMapping("/listChannels")
    public BaseWrapper listChannels(HttpServletRequest request, Integer siteId, Integer channelId){
        BaseWrapper json = new BaseWrapper();
        if (channelId == null) {
            channelId=0;
        }
        json.setData(cmsChannelService.listChannels(siteId, channelId));
        json.setFlag(true);
        json.setStatus(true);
        return json;
    }

    @ResponseBody
    @RequestMapping("/listTrashChannels")
    public BaseWrapper listTrashChannels(HttpServletRequest request, Integer siteId, Integer channelId){
        BaseWrapper json = new BaseWrapper();
        json.setData(cmsChannelService.listTrashChannels(siteId, channelId));
        json.setStatus(true);
        return json;
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/5/24
     * @说明: 根据栏目id 更新栏目状态
     */
    @ResponseBody
    @RequestMapping("/updateStatus")
    public BaseWrapper updateStatus(CmsChannelInfo cmsChannelInfo,String sids){
        return cmsChannelService.updateStatus(cmsChannelInfo,sids);
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/5/24
     * @说明: 根据站点id获取已发布的栏目
     */
    @ResponseBody
    @RequestMapping("/getChannelInfos")
    public BaseWrapper getChannelInfos(Integer siteId){
        return cmsChannelService.getChannelInfos(siteId);
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/5/30
     * @说明: 根据id获取栏目信息
     */
    @ResponseBody
    @RequestMapping("/geiChannelInfoById")
    public BaseWrapper geiChannelInfoById(Integer channelId){
        return cmsChannelService.geiChannelInfoById(channelId);
    }

    /**
     * @作者: 张航宁
     * @时间: 2019/06/04
     * @说明: 根据站点id获取已发布的栏目 以及包括五条最新信息 用于首页
     */
    @ResponseBody
    @RequestMapping("/getIndexChannelDocs")
    public BaseWrapper getIndexChannelDocs(Integer siteId){
        return cmsChannelService.getIndexChannelDocs(siteId);
    }

}
